package com.csdj.course.service.impl;


import com.csdj.course.mapper.CoursePriceMapper;
import com.csdj.course.pojo.Course;
import com.csdj.course.pojo.CourseAndPrice;
import com.csdj.course.pojo.CoursePrice;
import com.csdj.course.service.CoursePriceService;
import com.csdj.course.service.CourseService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

@Service
public class CoursePricePriceServiceImpl implements CoursePriceService {
	@Autowired
	private CoursePriceMapper coursePriceMapper;
	@Autowired
	private CourseService courseService;

	/**
	 * 查询课程价格全部结果
	 *
	 * @return
	 */
	@Override
	public List<CoursePrice> selectAll() {
		return coursePriceMapper.selectAll();
	}

	/**
	 * 根据编号查询价格结果
	 *
	 * @param courseId
	 * @return
	 */
	@Override
	public CoursePrice select(Integer courseId) {

		return coursePriceMapper.selectOne(new CoursePrice(courseId));
	}

	/**
	 *根据编号查询课程及价格全部结果
	 *
	 * @return
	 */
	@Override
	public List<CourseAndPrice> select() {
		List<Course> courseList	=courseService.selectAll();
		List<CourseAndPrice> coursePriceList= new ArrayList<>();
		for(Course course:courseList){
			if(course != null){
				CoursePrice coursePrice = select(course.getCourseId());
				System.out.println(coursePrice);
				CourseAndPrice courseAndPrice=new CourseAndPrice();
				courseAndPrice.setId(course.getId());
				courseAndPrice.setName(course.getName());
				courseAndPrice.setValid(course.getValid());
				courseAndPrice.setPrice(coursePrice.getPrice());
				coursePriceList.add(courseAndPrice);
			}
		}
		return coursePriceList;
	}

}
